package com.yesorno.ui;

import com.yesorno.activity.HallActivity;
import com.yesorno.activity.R;
import com.yesorno.activity.TopicListActivity;
import com.yesorno.activity.UserInfoActivity;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 底部菜单中的按钮
 * 
 * @author samoin
 * @date 2011.07.14
 * @version 0.1
 * 
 */
public class MenuLinearLayout extends LinearLayout {
	private int strId;
	private int selectedImgId;
	private int unSelectedImgId;

	public boolean isSelect = false;

	int selectedBgColor = 0x00B7B7B7;
	int unSelectedBgColor = 0xffB7B7B7;

	int selectedFontColor = 0xffFFFFFB;
	int unSelectedFontColor = 0xff4D4D4D;

	int curImgId;

	private ImageView imageView;
	private TextView textView;

	Context context;

	MenuOnClickListener clickListener = new MenuOnClickListener();

	public MenuLinearLayout(Context context) {
		super(context);
	}

	public MenuLinearLayout(Context context, Integer selectIndex) {
		super(context);
	}

	public MenuLinearLayout(Context context, int strId, int selectedImgId,
			int unSelectedImgId) {
		super(context);
		this.context = context;
		setLayoutPamras();
		this.strId = strId;
		this.selectedImgId = selectedImgId;
		this.unSelectedImgId = unSelectedImgId;
		this.setOrientation(VERTICAL);
		this.setGravity(Gravity.CENTER);
		addImage(context);
		addText(context);
	}

	/**
	 * 添加菜单文字
	 * 
	 * @param context
	 */
	private void addText(Context context) {
		textView = new TextView(context);
		textView.getPaint().setFakeBoldText(true);// 设置粗体
		textView.setText(strId);
		textView.setTextColor(unSelectedFontColor);
		textView.setGravity(Gravity.CENTER);
		this.addView(textView);
	}

	/**
	 * 添加菜单按钮
	 * 
	 * @param context
	 */
	private void addImage(Context context) {
		imageView = new ImageView(context);
		curImgId = unSelectedImgId;
		this.setBackgroundColor(unSelectedBgColor);
		imageView.setImageResource(unSelectedImgId);
		this.setOnClickListener(clickListener);
		this.addView(imageView);
	}

	/**
	 * 菜单点击的类
	 * 
	 * @author samoin
	 * @date 2011-07-15
	 * @version 0.1
	 * 
	 */
	class MenuOnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {

			switch (strId) {
			case R.string.bottom_menu_hall:// 大厅
				changeActivity(v, context, HallActivity.class);
				break;
			case R.string.bottom_menu_info:// 信息
				changeActivity(v, context, TopicListActivity.class);
				break;
			case R.string.bottom_menu_data:// 资料
				changeActivity(v, context, UserInfoActivity.class);
				break;
			case R.string.bottom_menu_more:// 更多

				break;
			case R.string.bottom_menu_search:// 搜索

				break;
			default:
				break;
			}
		}

		/**
		 * 菜单跳转
		 * 
		 * @param v
		 *            对应按钮的ImageView
		 * @param context
		 *            当前的context
		 * @param cls
		 *            要跳转到的Activity的class
		 */
		public void changeActivity(View v, Context context, Class<?> cls) {
			MenuLinearLayout mll = (MenuLinearLayout) (v);
			if (!mll.isSelect) {
				Intent intent = new Intent();
				intent.setClass(context, cls);
				context.startActivity(intent);
			}
		}
	}

	/**
	 * 根据当前的类型，更换菜单的背景、图片和颜色
	 */
	public void changeMenuBgType() {
		isSelect = isSelect ? false : true;
		curImgId = (curImgId == selectedImgId) ? unSelectedImgId
				: selectedImgId;
		imageView.setImageResource(curImgId);
		int bgcolor = (curImgId == selectedImgId) ? selectedBgColor
				: unSelectedBgColor;
		this.setBackgroundColor(bgcolor);
		int textcolor = (curImgId == selectedImgId) ? selectedFontColor
				: unSelectedFontColor;
		textView.setTextColor(textcolor);
	}

	/**
	 * 设置布局
	 */
	private void setLayoutPamras() {
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		lp.weight = 1;
		this.setLayoutParams(lp);
	}

	public MenuLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

}
